//app.js
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    var tasks = wx.getStorageSync('tasks');
    tasks?(this.globalData.tasks = tasks):(this.globalData.tasks = null)
    this.globalComFunction();
  },
  getUserInfo:function(cb){
    var that = this
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  getSystemInfo:function(cb){
    var that = this
    if(this.globalData.systemInfo){
      typeof cb == "function" && cb(this.globalData.systemInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function () {
          wx.getSystemInfo({
            success: function (res) {
              that.globalData.systemInfo = res
              typeof cb == "function" && cb(that.globalData.systemInfo)
            }
          })
        }
      })
    }
  },
  //code=0表示用户信息和系统信息
  //code="i"表示用户信息
  reloadUserInfo:function(target){
    if(target){
      var that = this;
      if(that.globalData.userInfo){
        target.setData({
          userInfo:that.globalData.userInfo
        })
      }else{
        that.getUserInfo(function(userInfo){
        //更新数据
          target.setData({
            userInfo:userInfo
          })
        })
      }
    }
  },
  //code="s"表示系统信息
  reloadSystemInfo:function(target){
    if(target){
      var that = this;
      if(that.globalData.systemInfo){
        target.setData({
          systemInfo:that.globalData.systemInfo
        })
      }else{
        that.getSystemInfo(function(systemInfo){
        //更新数据
          target.setData({
            systemInfo:systemInfo
          })
        })
      }
    }
  },
  //不区分大小写
  reloadInfos:function(target,code){
    if(target){
      var that = this;
      if(code==undefined){
        that.reloadUserInfo(target);
        that.reloadSystemInfo(target);
      }else if(code.toUpperCase=="I"){
        that.reloadUserInfo(target);
      }else if(code.toUpperCase=="S"){
        that.reloadSystemInfo(target);
      }
    }
  },
  globalComFunction:function(){
    Array.prototype.sortStartTime = function(param){
      //有一个参数，不输入则为开始时间倒序排列
      //输入esc则为开始时间正序排列
      //也可以自定义function'
      if(this){
        console.log('排序方法说明：\n有一个参数，不输入则为开始时间倒序排列\n输入esc则为开始时间正序排列\n也可以自定义function');
        if(param && typeof param == 'function'){
          this.sort(param);
        }else if(typeof param == 'string' && param.toLowerCase() == 'esc'){
          this.sort(function(a,b){
            return a.startTime - b.startTime;
          });
        }else{
          this.sort(function(a,b){
            return a.startTime - b.startTime;
          });
        }
      }
    }
    Array.prototype.insert = function (index, item) {
      //有两个参数(index,item)，index为坐标索引，item为对应对象
      //如果输入的索引index大于或等于数组长度，则默认添加到最后。
      //如果输入的索引index小于数组长度，则添加在索引前。'
      if(this){
        console.log('方法说明：\n有两个参数(index,item)，index为坐标索引，item为对应对象\n如果输入的索引index大于或等于数组长度，则默认添加到最后。\n如果输入的索引index小于数组长度，则添加在索引前。');
        if(index <= 0){
          index = 0;
        }
        if(index >= this.length){
          this.push(item);
        }else{
          this.splice(index, 0, item);
        }
      }
    };
  },
  findAddTaskLocation:function(target,added){
    var max = target.length;
    if(max==1){
      if(target[0].startTime < added.startTime){
        target.push(added);
      }else{
        target.insert(0,added);
      }
    }else{
      var location = parseInt(max/2);//索引开始
      var locationBF = location;//索引开始
      //二分法
      while(location <= max){
        if(target[location].startTime > added.startTime){
          if(location <= 0 || 
            (target[location - 1] && target[location - 1].startTime < added.startTime) ){
            target.insert(location,added);
            break;
          }else{
            location = location - parseInt((max - location)/2);
          }
        }else{
          if(location >= (max - 1) || 
             target[location + 1].startTime > added.startTime){
            target.insert(location + 1,added);
            break;
          }else{
            location = location + Math.ceil((max - location)/2);
          }
        }
      }
    }
    return target;
  },
  globalData:{
    ruleList:['天','组','个','页','公里','小时','分钟'],
    userInfo:null,
    tasks:null,
    systemInfo:null
  }
})